﻿<?php
require_once "db.class.php";
require_once "server.class.php";
require_once "config.php";

$params = array("host"=>"$host", "user"=>"$user", "pass"=>"$pass", "db"=>"$db");
$db = new Dbclass($params, false);
$server = new ServerHelperClass($db, $tables);

$microtimestamp = microtime(true);
$action = $_POST['action'];
$userId = $_POST['userId'];
$xmlContent = "";

//MD5验证，是否B2客户端发起
if ($server->generateSig($_POST, $secret) != $_POST['b2Sig'])
{
	//$debug2 = $server->generate_sig($_POST, $secret)." <-> ".$_POST['b2Sig'];
	die();
}

//后门，调试用
if ($action == 'alibaba')
{
	$sql = "update ".$tb_user_profile." set sessionId = ".$timeStamp." where id = ".$userId;
	$result = $db->query($sql);
	$sessionId = $timeStamp;
	$xmlContent .= "<sessionId><![CDATA[".$sessionId."]]></sessionId>\n";
	$return = "success";
}
else
{
	//检查session
	$sql = "select count(*) as num from ".$tb_user_profile." where id = ".$userId." and sessionId = '".$_POST['sessionId']."'";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	if ($arr['num'] <= 0)
	{
		$return = "other login";
	}
	else
	{
		//检查callId
		$sql = "insert into ".$tb_user_call." (ownerId, callId) values (".$userId.", ".$_POST['callId'].")";
		$result = $db->query($sql);
		$sql = "select count(*) as num from ".$tb_user_call." where ownerId = ".$userId." and callId = ".$_POST['callId'];
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		if ($arr['num'] > 1)
		{
			$return = "other login";
		}
	}
}
if ($return != "other login")
{
if ($action == "selectTank")
{
	$tankIndex = $_POST['tankIndex'];
	$sql = "update ".$tb_user_profile." a,".$tb_tank_profile." b set a.campTankId = b.id where a.id = ".$userId." and b.ownerId = ".$userId." and b.tankIndex = ".$tankIndex;
	$db->query($sql);
	$sql = "select campTankId from ".$tb_user_profile." where id = ".$userId;
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<campTankId><![CDATA[".$arr['campTankId']."]]></campTankId>\n";
	}
	$return = "success";
}
else if ($action == "finishGuide")
{
	$sql = "update ".$tb_user_profile." set finishGuide = true where id = ".$userId;
	$db->query($sql);
	$return = "success";
}
else if ($action == "checkNewSocialInfo")
{
	$sql = "select count(*) as socialCount from ".$tb_social_profile." where targetId = ".$userId." and viewed = false";
	$result = $db->query($sql);
	if (($arr = $db->fetchArray($result)) && $arr['socialCount'] > 0)
	{
		$xmlContent .= "<hasNewSocialInfo><![CDATA[1]]></hasNewSocialInfo>\n";
	}
	else
	{
		$xmlContent .= "<hasNewSocialInfo><![CDATA[0]]></hasNewSocialInfo>\n";
	}
	$debug = $sql;
	$return = "success";
}
else if ($action == "getSocialInfo")
{
	if ($_POST['allFlag'])
	{
		$sql = "select attackerId, targetId, honourChange, viewed, attackDate, b.nickname as attackerName, c.tankIndex, c.basicLevel, c.strengthenLevel from ".$tb_social_profile." a inner join ".$tb_user_profile." b on a.attackerId = b.id inner join ".$tb_tank_profile." c on b.campTankId = c.id where targetId = ".$userId." order by attackDate desc limit 0,50";
	}
	else
	{
		$sql = "select attackerId, targetId, honourChange, viewed, attackDate, b.nickname as attackerName from ".$tb_social_profile." a inner join ".$tb_user_profile." b on a.attackerId = b.id where targetId = ".$userId." and viewed = false order by attackDate desc limit 0,10";
	}
	$result = $db->query($sql);
	$xmlContent .= "<socialPackage>\n";
	while ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<social>\n";
		$xmlContent .= "<attackerId><![CDATA[".$arr['attackerId']."]]></attackerId>\n";
		$xmlContent .= "<attackerName><![CDATA[".$arr['attackerName']."]]></attackerName>\n";
		$xmlContent .= "<attackerTankIndex><![CDATA[".$arr['tankIndex']."]]></attackerTankIndex>\n";
		$xmlContent .= "<attackerTankBasicLevel><![CDATA[".$arr['basicLevel']."]]></attackerTankBasicLevel>\n";
		$xmlContent .= "<attackerTankStrengthenLevel><![CDATA[".$arr['strengthenLevel']."]]></attackerTankStrengthenLevel>\n";
		$xmlContent .= "<targetId><![CDATA[".$arr['targetId']."]]></targetId>\n";
		$xmlContent .= "<honourChange><![CDATA[".$arr['honourChange']."]]></honourChange>\n";
		$xmlContent .= "<viewed><![CDATA[".$arr['viewed']."]]></viewed>\n";
		$xmlContent .= "<attackDate><![CDATA[".$arr['attackDate']."]]></attackDate>\n";
		$xmlContent .= "</social>\n";
	}
	$xmlContent .= "</socialPackage>\n";
	$sql = "update ".$tb_social_profile." set viewed = true where targetId = ".$userId;
	$result = $db->query($sql);
}
else if ($action == "getPlayerInfo")
{
	$playerId = $_POST['playerId'];
	$sql = "select count(*) as attackedCount from ".$tb_social_profile." where targetId = ".$playerId." and dayofyear(attackDate) = dayofyear(CURRENT_TIMESTAMP)";
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$attackedCount = $arr['attackedCount'];
	}
	$sql = "select campTankId, openId, nickname from ".$tb_user_profile." where id = ".$playerId;
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$xmlContent .= "<playerInfo>\n";
		$xmlContent .= "<playerId><![CDATA[".$playerId."]]></playerId>\n";
		$xmlContent .= "<campTankId><![CDATA[".$arr['campTankId']."]]></campTankId>\n";
		$xmlContent .= "<openId><![CDATA[".$arr['openId']."]]></openId>\n";
		$xmlContent .= "<nickname><![CDATA[".$arr['nickname']."]]></nickname>\n";
		$xmlContent .= "<attackedCount><![CDATA[".$attackedCount."]]></attackedCount>\n";
		$xmlContent .= "</playerInfo>\n";
	}
	$xmlContent .= $server->getTankList($playerId);
	$xmlContent .= $server->getObjList($playerId);
}
else if ($action == "getPackageAndTank")
{
	$xmlContent .= $server->getTankList($userId);
	$xmlContent .= $server->getObjList($userId);
	$xmlContent .= $server->getMailAndGiftList($userId);
}
else if ($action == "sellObject")
{
	$tankId = $_POST['tankId'];
	$objectIndex = $_POST['objectIndex'];
	$basicLevel = $_POST['basicLevel'];
	$strengthenLevel = $_POST['strengthenLevel'];
	$objectNum = $_POST['objectNum'];
	$sellOil = $_POST['priceOil'] * $objectNum;
	//检查是否有足够物品
	if ($objectIndex > 9999 && $objectIndex < 20000)
	{
		$sql = "select count(*) as objectNum from ".$tb_tank_profile." where id = ".$tankId;
	}
	else
	{
		$sql = "select objectNum from ".$tb_object_profile." where ownerId = ".$userId." and objectIndex = ".$objectIndex." and basicLevel = ".$basicLevel." and strengthenLevel = ".$strengthenLevel." and tankId = 0";
	}
	$debug = $sql;
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$currentNum = $arr['objectNum'];
	if ($currentNum < $objectNum)
	{
		$return = "not enough object";
	}
	else
	{
		//坦克
		if ($objectIndex > 9999 && $objectIndex < 20000)
		{
			$sql = "select objectIndex, basicLevel, strengthenLevel from ".$tb_object_profile." where ownerId = ".$userId." and tankId = ".$tankId;
			$result = $db->query($sql);
			while ($arr = $db->fetchArray($result))
			{
				$server->getOffEquip($userId, $arr['objectIndex'], $arr['basicLevel'], $arr['strengthenLevel'], $tankId);
			}
			$sql = "delete from ".$tb_tank_profile." where id = ".$tankId;
			$db->query($sql);
		}
		//装备、物品
		else
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum - ".$objectNum." where ownerId = ".$userId." and objectIndex = ".$objectIndex." and basicLevel = ".$basicLevel." and strengthenLevel = ".$strengthenLevel." and tankId = 0";
			$db->query($sql);
		}
		$sql = "update ".$tb_object_profile." set objectNum = objectNum + ".$sellOil." where ownerId = ".$userId." and objectIndex = 0";
		$db->query($sql);
		$return = "success";
	}
	//获得清单
	$xmlContent .= $server->getObjList($userId);
	$xmlContent .= $server->getTankList($userId);
}
else if ($action == "purchaseObject")
{
	$objectIndex = $_POST['objectIndex'];
	$objectNum = $_POST['objectNum'];
	$needOil = $_POST['priceOil'] * $objectNum;
	$needGas = $_POST['priceGas'] * $objectNum;
	$needHonour = $_POST['priceHonour'] * $objectNum;

	if ($needOil > 0)
	{
		$sql = "select objectNum from ".$tb_object_profile." where ownerId = ".$userId." and objectIndex = 0";
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		$currentOil = $arr['objectNum'];
	}
	else if ($needGas > 0)
	{
		$sql = "select objectNum from ".$tb_object_profile." where ownerId = ".$userId." and objectIndex = 1";
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		$currentGas = $arr['objectNum'];
	}
	else
	{
		$sql = "select objectNum from ".$tb_object_profile." where ownerId = ".$userId." and objectIndex = 10";
		$result = $db->query($sql);
		$arr = $db->fetchArray($result);
		$currentHonour = $arr['objectNum'];
	}
	//检查是否有足够的石油或者天然气
	if ($needOil > 0 && $needOil > $currentOil)
	{
		$return = "not enough oil";
	}
	else if ($needGas > 0 && $needGas > $currentGas)
	{
		$return = "not enough gas";
	}
	else if ($needHonour > 0 && $needHonour > $currentHonour)
	{
		$return = "not enough honour";
	}
	else
	{
		//坦克
		if ($objectIndex > 9999 && $objectIndex < 20000)
		{
			$sql = "insert into ".$tb_tank_profile." (ownerId, tankIndex) values (".$userId.", ".$objectIndex.")";
			for ($i = 0; $i < $objectNum - 1; $i = $i + 1)
			{
				$sql .= ", (".$userId.",".$objectIndex.")";
			}
			$result = $db->query($sql);
		}
		//装备、物品
		else
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum + ".$objectNum." where ownerId = ".$userId." and objectIndex = ".$objectIndex." and basicLevel = 0 and strengthenLevel = 0 and tankId = 0";
			$db->query($sql);
			if ($db->affectedRows() <= 0)
			{
				$sql = "insert into ".$tb_object_profile." (ownerId, objectIndex, objectNum) values (".$userId.", ".$objectIndex.", ".$objectNum.")";
				$db->query($sql);
			}
		}
		//付账
		if ($needOil > 0)
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum - ".$needOil." where ownerId = ".$userId." and objectIndex = 0";
			$db->query($sql);
		}
		else if ($needGas > 0)
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum - ".$needGas." where ownerId = ".$userId." and objectIndex = 1";
			$db->query($sql);
		}
		else
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum - ".$needHonour." where ownerId = ".$userId." and objectIndex = 10";
			$db->query($sql);
		}
		$return = "success";
	}
	//获得清单
	$xmlContent .= $server->getObjList($userId);
	$xmlContent .= $server->getTankList($userId);
}
else if ($action == "strengthenObj")
{
	$objIndex = $_POST['objectIndex'];
	$objBasicLevel = $_POST['basicLevel'];
	$objStrengthenLevel = $_POST['strengthenLevel'];
	$objTankId = $_POST['tankId'];
	$successFlag = $_POST['successFlag'];
	if ($successFlag)
	{
		$sql = "update ".$tb_object_profile." set objectNum = objectNum - 1 where objectNum > 0 and objectIndex = ".$objIndex." and basicLevel = ".$objBasicLevel." and strengthenLevel = ".$objStrengthenLevel." and tankId = ".$objTankId." and ownerId = ".$userId;
		$result = $db->query($sql);
		if ($db->affectedRows() > 0)
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum + 1 where objectIndex = ".$objIndex." and basicLevel = ".$objBasicLevel." and strengthenLevel = ".$objStrengthenLevel." + 1 and tankId = ".$objTankId." and ownerId = ".$userId;
			$result = $db->query($sql);
			if ($db->affectedRows() <= 0)
			{
				$sql = "insert into ".$tb_object_profile." (ownerId, objectIndex, basicLevel, strengthenLevel, tankId, objectNum) values (".$userId.", ".$objIndex.", ".$objBasicLevel.", ".$objStrengthenLevel." + 1, 0, 1)";
				$db->query($sql);
			}
			$return = "success";
		}
		else
		{
			$return = "fail";
		}
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "strengthenTank")
{
	$tankIndex = $_POST['objectIndex'];
	$basicLevel = $_POST['basicLevel'];
	$strengthenLevel = $_POST['strengthenLevel'];
	$tankId = $_POST['tankId'];
	$successFlag = $_POST['successFlag'];
	if ($successFlag)
	{
		$sql = "update ".$tb_tank_profile." set strengthenLevel = ".$strengthenLevel." + 1 where id = ".$tankId;
		$db->query($sql);
		$return = "success";
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "fuseObj")
{
	$objIndex = $_POST['objectIndex'];
	$objBasicLevel = $_POST['basicLevel'];
	$objStrengthenLevel = $_POST['strengthenLevel'];
	$objTankId = $_POST['tankId'];
	$successFlag = $_POST['successFlag'];
	if ($successFlag)
	{
		$sql = "update ".$tb_object_profile." set objectNum = objectNum - 2 where objectNum > 1 and objectIndex = ".$objIndex." and basicLevel = ".$objBasicLevel." and strengthenLevel = ".$objStrengthenLevel." and tankId = ".$objTankId." and ownerId = ".$userId;
		$result = $db->query($sql);
		if ($db->affectedRows() > 0)
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum + 1 where objectIndex = ".$objIndex." and basicLevel = ".$objBasicLevel." + 1 and strengthenLevel = 0 and tankId = ".$objTankId." and ownerId = ".$userId;
			$result = $db->query($sql);
			if ($db->affectedRows() <= 0)
			{
				$sql = "insert into ".$tb_object_profile." (ownerId, objectIndex, basicLevel, strengthenLevel, tankId, objectNum) values (".$userId.", ".$objIndex.", ".$objBasicLevel." + 1, 0, 0, 1)";
				$db->query($sql);
			}
			$xmlContent .= "<objIndex><![CDATA[".$objIndex."]]></objIndex>\n";
			$xmlContent .= "<basicLevel><![CDATA[".$objBasicLevel."]]></basicLevel>\n";
			$xmlContent .= "<strengthenLevel><![CDATA[".$objStrengthenLevel."]]></strengthenLevel>\n";
			$return = "success";
		}
		else
		{
			$return = "fail";
		}
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "fuseTank")
{
	$tankIndex = $_POST['objectIndex'];
	$basicLevel = $_POST['basicLevel'];
	$strengthenLevel = $_POST['strengthenLevel'];
	$tankId1 = $_POST['tankId1'];
	$tankId2 = $_POST['tankId2'];
	$successFlag = $_POST['successFlag'];
	if ($successFlag)
	{
		$sql = "select objectIndex, basicLevel, strengthenLevel from ".$tb_object_profile." where ownerId = ".$userId." and tankId = ".$tankId2;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$server->getOffEquip($userId, $arr['objectIndex'], $arr['basicLevel'], $arr['strengthenLevel'], $tankId2);
		}
		$sql = "delete from ".$tb_tank_profile." where id = ".$tankId2;
		$db->query($sql);
		if ($db->affectedRows() > 0)
		{
			$sql = "update ".$tb_tank_profile." set basicLevel = ".$basicLevel." + 1, strengthenLevel = 0 where id = ".$tankId1;
			$db->query($sql);
			$xmlContent .= "<objIndex><![CDATA[".$tankIndex."]]></objIndex>\n";
			$xmlContent .= "<basicLevel><![CDATA[".$basicLevel."]]></basicLevel>\n";
			$xmlContent .= "<strengthenLevel><![CDATA[".$strengthenLevel."]]></strengthenLevel>\n";
			$return = "success";
		}
		else
		{
			$return = "fail";
		}
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "mixtureTank")
{
	$tankId1 = $_POST['tankId1'];
	$tankId2 = $_POST['tankId2'];
	$tankIndex = $_POST['objectIndex'];
	$basicLevel = $_POST['basicLevel'];
	$strengthenLevel = $_POST['strengthenLevel'];
	$successFlag = $_POST['successFlag'];
	if ($successFlag)
	{
		$sql = "select objectIndex, basicLevel, strengthenLevel from ".$tb_object_profile." where ownerId = ".$userId." and tankId = ".$tankId2;
		$result = $db->query($sql);
		while ($arr = $db->fetchArray($result))
		{
			$server->getOffEquip($userId, $arr['objectIndex'], $arr['basicLevel'], $arr['strengthenLevel'], $tankId2);
		}
		$sql = "delete from ".$tb_tank_profile." where id = ".$tankId2;
		$db->query($sql);
		if ($db->affectedRows() > 0)
		{
			$sql = "update ".$tb_tank_profile." set tankIndex = ".$tankIndex.", basicLevel = ".$basicLevel.", strengthenLevel = ".$strengthenLevel." where id = ".$tankId1;
			$db->query($sql);
			$xmlContent .= "<objIndex><![CDATA[".$tankIndex."]]></objIndex>\n";
			$xmlContent .= "<basicLevel><![CDATA[".$basicLevel."]]></basicLevel>\n";
			$xmlContent .= "<strengthenLevel><![CDATA[".$strengthenLevel."]]></strengthenLevel>\n";
			$return = "success";
		}
		else
		{
			$return = "fail";
		}
	}
	else
	{
		$return = "fail";
	}
}
else if ($action == "postEmail")
{
	$jsonString=urldecode($_POST['mail']);
 	$jsonString=str_replace("//","",$jsonString);
 	$data=json_decode($jsonString,true);
 	$fromId = $data['fromId'];
 	$fromName = $data['fromName'];
	$toId = $data['toId'];
	$sql = "select id from ".$tb_user_profile." where openId = '".$toId."'";
	$result = $db->query($sql);
	if ($arr = $db->fetchArray($result))
	{
		$toId = $arr['id'];
	}
	$message = $data['message'];
	$debug = $jsonString;
	$sql = "insert into ".$tb_mail_profile." (fromId, toId, message, fromName) values (".$fromId.",".$toId.",'".$message."','".$fromName."')";
	$result = $db->query($sql);
	$sql = "select last_insert_id()";
	$result = $db->query($sql);
	$arr = $db->fetchArray($result);
	$mailId = $arr['last_insert_id()'];
	$giftArray = (array)$data['giftArray'];
	$num_elements=count($giftArray); //这时$num_elements的值为3
	for ($i=0; $i<$num_elements; ++$i)
	{
		$gift = $giftArray[$i];
		$obj = $gift['obj'];
		$tankId = $obj['tankId'];
		$num = $gift['num'];
		$objectIndex = $obj['objectIndex'];
		$basicLevel = $obj['basicLevel'];
		$strengthenLevel = $obj['strengthenLevel'];
		$sql = "insert into ".$tb_gift_profile." (mailId, objectIndex, objectNum, basicLevel, strengthenLevel) values (".$mailId.",".$objectIndex.",".$num.",".$basicLevel.",".$strengthenLevel.")";
		$result = $db->query($sql);
		if ($tankId != 0)
		{
			$sql = "select objectIndex, basicLevel, strengthenLevel from ".$tb_object_profile." where ownerId = ".$userId." and tankId = ".$tankId;
			$result = $db->query($sql);
			while ($arr = $db->fetchArray($result))
			{
				$server->getOffEquip($userId, $arr['objectIndex'], $arr['basicLevel'], $arr['strengthenLevel'], $tankId);
			}
			$sql = "delete from ".$tb_tank_profile." where id = ".$tankId;
			$db->query($sql);
		}
		
	}
	$return = "success";
}

else if ($action == "getMail")
{
	$mailId = $_POST['mailId'];
	$userId = $_POST['userId'];
	$sql = "select objectIndex, basicLevel, strengthenLevel, objectNum from ".$tb_gift_profile." where mailId = ".$mailId;
	$result = $db->query($sql);
	
	while($arr = $db->fetchArray($result))
	{
		$objectIndex = $arr['objectIndex'];
		$basicLevel = $arr['basicLevel'];
		$strengthenLevel = $arr['strengthenLevel'];
		$objectNum = $arr['objectNum'];
		//坦克
		if ($objectIndex > 9999 && $objectIndex < 20000)
		{
			$sql = "insert into ".$tb_tank_profile." (ownerId, tankIndex, basicLevel, strengthenLevel) values (".$userId.", ".$objectIndex.",".$basicLevel.",".$strengthenLevel.")";
			for ($i = 0; $i < $objectNum - 1; $i = $i + 1)
			{
				$sql .= ", (".$userId.",".$objectIndex.",".$basicLevel.",".$strengthenLevel.")";
			}
			$result = $db->query($sql);
		}
		else
		{
			$sql = "update ".$tb_object_profile." set objectNum = objectNum + ".$objectNum." where ownerId = ".$userId." and objectIndex = ".$objectIndex." and basicLevel = ".$basicLevel." and strengthenLevel = ".$strengthenLevel." and tankId = 0";
			$debug = $sql;
			$db->query($sql);
			if ($db->affectedRows() <= 0)
			{
				$sql = "insert into ".$tb_object_profile." (ownerId, objectIndex, basicLevel, strengthenLevel, objectNum) values (".$userId.", ".$objectIndex.",".$basicLevel.",".$strengthenLevel.", ".$objectNum.")";
				$db->query($sql);
			}
		}
	}
	$sql = "delete from ".$tb_gift_profile." where mailId = ".$mailId;
	$result = $db->query($sql);
	$sql = "delete from ".$tb_mail_profile." where id = ".$mailId;
	$result = $db->query($sql);
	$xmlContent .= $server->getObjList($userId);
	$xmlContent .= $server->getTankList($userId);
	$xmlContent .= $server->getMailAndGiftList($userId);
	$return = "success";
}
//更换出战坦克
else if ($action == "changeCampTank")
{
	$tankId = $_POST['tankId'];
	$sql = "update ".$tb_user_profile." set campTankId = ".$tankId." where id = ".$userId;
	$db->query($sql);
	$xmlContent .= "<campTankId><![CDATA[".$tankId."]]></campTankId>\n";
}
else if ($action == "changeEquip")
{
	$originalEquipIndex = $_POST['originalEquipIndex'];
	if ($originalEquipIndex != "")
	{
		$originalEquipBasicLevel = $_POST['originalEquipBasicLevel'];
		$originalEquipStrengthenLevel = $_POST['originalEquipStrengthenLevel'];
		$originalEquipTankId = $_POST['originalEquipTankId'];
		$tankId = $originalEquipTankId;
		$server->getOffEquip($userId, $originalEquipIndex, $originalEquipBasicLevel, $originalEquipStrengthenLevel, $originalEquipTankId);
	}
	$newEquipIndex = $_POST['newEquipIndex'];
	if ($newEquipIndex != "")
	{
		$newEquipBasicLevel = $_POST['newEquipBasicLevel'];
		$newEquipStrengthenLevel = $_POST['newEquipStrengthenLevel'];
		$newEquipTankId = $_POST['newEquipTankId'];
		$tankId = $newEquipTankId;
		$sql = "update ".$tb_object_profile." set objectNum = objectNum - 1 where ownerId = ".$userId." and objectIndex = ".$newEquipIndex." and basicLevel = ".$newEquipBasicLevel." and strengthenLevel = ".$newEquipStrengthenLevel." and tankId = 0";
		$result = $db->query($sql);
		if ($db->affectedRows() > 0)
		{
			$sql = "insert into ".$tb_object_profile." (ownerId, objectIndex, basicLevel, strengthenLevel, tankId, objectNum) values (".$userId.",".$newEquipIndex.",".$newEquipBasicLevel.",".$newEquipStrengthenLevel.",".$newEquipTankId.",1)";
			$result = $db->query($sql);
		}
	}
	$xmlContent .= $server->getTankList($userId);
	$xmlContent .= $server->getObjList($userId);
	$return = "success";
}
//同步战力
else if ($action == "syncFightPoint")
{
	//更新战力
	$fightPoint = $_POST['fightPoint'];
	if ($fightPoint != "")
	{
		$sql = "update ".$tb_object_profile." set objectNum = ".$fightPoint." where ownerId = ".$userId." and objectIndex = 9";
		$result = $db->query($sql);
	}
}
//获得物品清单
else if ($action == "getPackageInfo")
{
	$playerId = $_POST['playerId'];
	$xmlContent .= $server->getObjList($playerId);
	$return = "success";
}
//获得任务状态
else if ($action == "getTask")
{
	$xmlContent .= $server->getTaskList($userId);
	$xmlContent .= $server->getCampList($userId);
	$return = "success";
}
//领取任务
else if ($action == "acceptTask")
{
	$taskIndex = $_POST['taskIndex'];
	$sql = "insert into ".$tb_task_profile." (taskIndex, ownerId) values (".$taskIndex.", ".$userId.")";
	$result = $db->query($sql);
	$campIndex = $_POST['campIndex'];
	if ($campIndex >= 0)
	{
		$sql = "insert into ".$tb_camp_profile." (campIndex, ownerId) values (".$campIndex.", ".$userId.")";
		$result = $db->query($sql);
	}
	$xmlContent .= $server->getTaskList($userId);
	$return = "success";
}
//完成任务
else if ($action == "accompTask")
{
	$taskIndex = $_POST['taskIndex'];
	$sql = "update ".$tb_task_profile." set taskStatus = 2 where ownerId = ".$userId." and taskIndex = ".$taskIndex;
	$result = $db->query($sql);
	$xmlContent .= $server->getTaskList($userId);
	$return = "success";
}
//检查比赛资格
else if ($action == "checkMatchQualification")
{
	$matchIndex = $_POST['matchIndex'];
	$sql = "select count(*) as count from ".$tb_match_profile." where matchIndex =".$matchIndex." and dayofyear(attendDate) = dayofyear(CURRENT_TIMESTAMP) and type = 1 and userId = ".$userId;
	$result = $db->query($sql);
	$xmlContent .= "<matchIndex><![CDATA[".$matchIndex."]]></matchIndex>\n";
	if (($arr = $db->fetchArray($result)) && $arr['count'] < 2)
	{
		$xmlContent .= "<qualified><![CDATA[1]]></qualified>\n";
	}
	else
	{
		$xmlContent .= "<qualified><![CDATA[0]]></qualified>\n";
	}
	$return = "success";
}
//获得比赛排名
else if ($action == "getMatchInfo")
{
	$matchIndex = $_POST['matchIndex'];
	for ($i = 0; $i < 2; $i++)
	{
		$sqltemp = array("weekofyear(attendDate) = weekofyear(CURRENT_TIMESTAMP) and type = 1", "type = 3");
		$sql = "select userId, matchScore, attendDate, b.nickname, campTankId, c.tankIndex, c.basicLevel, c.strengthenLevel from ".$tb_match_profile." a inner join ".$tb_user_profile." b on a.userId = b.id inner join ".$tb_tank_profile." c on b.campTankId = c.id where matchIndex = ".$matchIndex." and ".$sqltemp[$i]." order by matchScore desc limit 0,50";
		$result = $db->query($sql);
		$ranking = 0;
		$xmlContent .= "<matchInfo>\n";
		while ($arr = $db->fetchArray($result))
		{
			$xmlContent .= "<matchRanking>\n";
			$xmlContent .= "<playerId><![CDATA[".$arr['userId']."]]></playerId>\n";
			$xmlContent .= "<ranking><![CDATA[".$ranking."]]></ranking>\n";
			$xmlContent .= "<matchScore><![CDATA[".$arr['matchScore']."]]></matchScore>\n";
			$xmlContent .= "<attendDate><![CDATA[".$arr['attendDate']."]]></attendDate>\n";
			$xmlContent .= "<nickname><![CDATA[".$arr['nickname']."]]></nickname>\n";
			$xmlContent .= "<campTankId><![CDATA[".$arr['campTankId']."]]></campTankId>\n";
			$xmlContent .= "<tankIndex><![CDATA[".$arr['tankIndex']."]]></tankIndex>\n";
			$xmlContent .= "<basicLevel><![CDATA[".$arr['basicLevel']."]]></basicLevel>\n";
			$xmlContent .= "<strengthenLevel><![CDATA[".$arr['strengthenLevel']."]]></strengthenLevel>\n";
			$xmlContent .= "</matchRanking>\n";
			$ranking++;
		}
		$xmlContent .= "</matchInfo>\n";
	}
	$return = "success";
}
//结束比赛
else if ($action == "endMatch")
{
	$matchIndex = $_POST['matchIndex'];
	$matchScore = $_POST['matchScore'];
	$goalFlag = $_POST['goalFlag'];
	$xmlContent .= "<matchIndex><![CDATA[".$matchIndex."]]></matchIndex>\n";
	for ($i = 0; $i < 2; $i++)
	{
		$sqltemp = array("weekofyear(attendDate) = weekofyear(CURRENT_TIMESTAMP) and type = 1", "type = 3");
		$sql = "select count(*) as ranking from ".$tb_match_profile." where matchIndex = ".$matchIndex." and ".$sqltemp[$i]." and matchScore >= ".$matchScore;
		$result = $db->query($sql);
		$ranking = 0;
		if ($arr = $db->fetchArray($result))
		{
			$ranking = $arr["ranking"];
		}
		if ($ranking < 50)
		{
			$type = $i * 2 + 1;
			$sql = "insert into ".$tb_match_profile." (matchIndex, matchScore, userId, type) values (".$matchIndex.", ".$matchScore.", ".$userId.", ".$type.")";
			$result = $db->query($sql);
			$xmlContent .= "<ranking><![CDATA[".$ranking."]]></ranking>\n";
		}
		else
		{
			$xmlContent .= "<ranking><![CDATA[-1]]></ranking>\n";
		}
	}
	$return = "success";
}
//结束好友对战
else if ($action == "endFriendFight")
{
	$targetId = $_POST['targetId'];
	$honourChange = $_POST['honourChange'];
	$goalFlag = $_POST['goalFlag'];
	$sql = "insert into ".$tb_social_profile." (attackerId, targetId, honourChange) values (".$userId.", ".$targetId.", ".$honourChange.")";
	$result = $db->query($sql);
	$return = "success";
}
//结束战斗
else if ($action == "endFight")
{
	$campIndex = $_POST['campIndex'];
	$goalFlag = $_POST['goalFlag'];
	if ($goalFlag > 0)
	{
		$sql = "update ".$tb_camp_profile." set campNum = campNum + 1 where ownerId = ".$userId." and campIndex = ".$campIndex;
		$db->query($sql);
		$xmlContent .= $server->getCampList($userId);
	}
	$return = "success";
}
//获得荣誉排名
else if ($action == "getRanking")
{
	$xmlContent .= $server->getRankList(9);
	$xmlContent .= $server->getRankList(2);
	$xmlContent .= $server->getRankList(3);
	$return = "success";
}
//获得升级礼物
else if ($action == "fetchUpgradeGift")
{
	$level = $_POST['level'];
	$sql = "update ".$tb_user_profile." set currentLevel = ".$level." where id = ".$userId." and currentLevel < ".$level;
	$db->query($sql);
	$return = "success";
}
//获得新手礼物
else if ($action == "fetchNewPlayerGift")
{
	$sql = "update ".$tb_user_profile." set fetchNewPlayerGift = true where id = ".$userId;
	$db->query($sql);
	$return = "success";
}
//获得礼品
else if ($action == "fetchGift")
{
	$sql = "update ".$tb_user_profile." set fetchGiftDate = ".$timeStamp." where id = ".$userId;
	$db->query($sql);
	$xmlContent .= "<fetchGiftDate><![CDATA[".$timeStamp."]]></fetchGiftDate>\n";
	$return = "success";
}
//保存系统信息
else if ($action == "saveSystemInfo")
{
	$effectVolume = $_POST['effectVolume'];
	$musicVolume = $_POST['musicVolume'];
	$sql = "update ".$tb_system_profile." set effectVolume = ".$effectVolume.", musicVolume = ".$musicVolume." where ownerId = ".$userId;
	$db->query($sql);
	$return = "success";
}

$objectBalance = $_POST['objectBalance'];
if ($objectBalance != "")
{
	$xmlContent .= $server->balanceObject($objectBalance);
}

}
$db->close();

$phpElapsedTime = (microtime(true) - $microTimeStamp) * 1000;

$txt = "";
$txt .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$txt .= "<values>\n";
$txt .= "<debug><![CDATA[".$debug."]]></debug>\n";
$txt .= "<action><![CDATA[".$action."]]></action>\n";
$txt .= "<result><![CDATA[".$return."]]></result>\n";
$txt .= "<timeStamp><![CDATA[".$timeStamp."]]></timeStamp>\n";
$txt .= "<phpElapsedTime><![CDATA[".$phpElapsedTime."]]></phpElapsedTime>\n";
$txt .= $xmlContent;
$txt .= "</values>";

echo $txt;

?>
